package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.*;
import com.itheima.travel.pojo.*;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.CategoryVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.req.SellerVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.RedisCacheService;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.session.SubjectSellerContext;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @Description：线路模块类
 */
@Service
public class RouteServiceImpl implements RouteService {

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SellerMapper sellerMapper;

    @Autowired
    private AffixMapper affixMapper;

    @Autowired
    SubjectSellerContext subjectSellerContext;

    @Autowired
    RedisCacheService redisCacheService;

    @Autowired
    AffixService affixService;


    /**
     *  添加路线
     * @param routeVo  路径请求参数
     * @return  影响行数
     */
    @Override
    public Integer addRoute(RouteVo routeVo) {
        //保存
        Route route = BeanConv.toBean(routeVo, Route.class);
        return routeMapper.insert(route);
    }

    /**
     * 编辑路线
     * @param routeVo 线路请求参数
     * @return 影响行数
     */
    @Override
    public Integer updateRoute(RouteVo routeVo) {
        if (EmptyUtil.isNullOrEmpty(routeVo.getId())){
            return 0;
        }
        Route route = BeanConv.toBean(routeVo, Route.class);
        int flag = routeMapper.updateByPrimaryKey(route);

        //添加redis缓存
        //修改完成后，删除之前缓存对象,让第一次查询的人放入缓存
        if (flag==1){
            redisCacheService.deleSingleCache(RedisConstant.ROUTESERVICE_FINDROUTEBYID+routeVo.getId());
            //删除图片
            redisCacheService.deleListCache(RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID +routeVo.getId());
        }
        return flag;
    }

    /**
     * @Description 查询线路
     * @param routeVo 路径请求参数
     * @return 线路详情
     */
    @Override
    public RouteVo findRouteById(RouteVo routeVo) {

        //添加redis缓存
        return redisCacheService.singleCache(()->{
            RouteVo routeVoResult = BeanConv.toBean(routeMapper.selectByPrimaryKey(routeVo.getId()), RouteVo.class);
            //走缓存那图片
            AffixVo affixVo = AffixVo.builder().businessId(routeVoResult.getId()).build();
            routeVoResult.setAffixVoList(affixService.findAffixByBusinessId(affixVo));
            return routeVoResult;
        },RedisConstant.ROUTESERVICE_FINDROUTEBYID+routeVo.getId());

//        //查询线路基本信息
//        Route route = routeMapper.selectByPrimaryKey(routeVo.getId());
//        RouteVo resultRouteVo = BeanConv.toBean(route, RouteVo.class);
//        //查询线路所属的分类信息
//        Category category = categoryMapper.selectByPrimaryKey(Integer.parseInt(route.getCategoryId() + ""));
//        //封装结果到resultRouteVo对象中
//        resultRouteVo.setCategoryVo(BeanConv.toBean(category, CategoryVo.class));
//
//        //查询线路所属商家信息
//        Seller seller = sellerMapper.selectByPrimaryKey(route.getSellerId());
//        //封装结果到resultRouteVo对象中
//        resultRouteVo.setSellerVo(BeanConv.toBean(seller, SellerVo.class));
//
//        //查询线路包含的图片信息
//        AffixExample affixExample = new AffixExample();
//        affixExample.createCriteria().andBusinessIdEqualTo(route.getId());
//        List<Affix> affixList = affixMapper.selectByExample(affixExample);
//        //封装结果到resultRouteVo对象中
//        resultRouteVo.setAffixVoList(BeanConv.toBeanList(affixList, AffixVo.class));
//        return resultRouteVo;
    }

    /**
     * @Description 分页查询
     * @param routeVo 路径请求参数
     * @return 线路分页
     */
    @Override
    public PageInfo<RouteVo> findRouteByPage(RouteVo routeVo, int pageNum, int pageSize) {

        //设置redis缓存
        String key = RedisConstant.ROUTESERVICE_FINDROUTEBYPAGE + pageNum + ":" + pageSize;
        return redisCacheService.singleCache(()->{
            // 首先将当前页页码以及每页显示条数交个 pagehelper
            //将pageNum，pageSize绑定到当前线程对象上
            PageHelper.startPage(pageNum,pageSize);
            RouteExample routeExample = new RouteExample();
            RouteExample.Criteria criteria = routeExample.createCriteria();
            //多条件查询
            //拼接分类信息
            if (!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())){
                criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
            }
            //拼接路线信息
            if (!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
                criteria.andRouteNameLike("%" + routeVo.getRouteName());
            }
            //是否主体之旅
            if (!EmptyUtil.isNullOrEmpty(routeVo.getIsThemeTour())){
                criteria.andIsThemeTourEqualTo(routeVo.getIsThemeTour());
            }
            //拼接最低价格
            if (!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())){
                criteria.andPriceGreaterThan(routeVo.getMinPrice());
            }
            //拼接最高价格
            if (!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())){
                criteria.andPriceLessThan(routeVo.getMaxPrice());
            }
            //注意这里的字段为数据库字段：类似example.setOrderByClause("index ASC,created_time ASC");
//        if (!EmptyUtil.isNullOrEmpty(routeVo.getOrderBy())){
//            example.setOrderByClause(routeVo.getOrderBy());
//        }

            List<Route> routes = routeMapper.selectByExample(routeExample);
            PageInfo<Route> pageInfo = new PageInfo<>(routes);
            PageInfo<RouteVo> pageInfoVo = new PageInfo<>();
            //外部对象拷贝
            BeanConv.toBean(pageInfo,pageInfoVo);
            //结果集转换
            List<RouteVo> routeVoList = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
            pageInfoVo.setList(routeVoList);
            //处理图片
            for (RouteVo routeVoHandler : routeVoList) {
                AffixVo affixVo = AffixVo.builder().businessId(routeVoHandler.getId()).build();
                routeVoHandler.setAffixVoList(affixService.findAffixByBusinessId(affixVo));
            }
            return pageInfoVo;

        },key);
        /*
        // 首先将当前页页码以及每页显示条数交个 pagehelper
        //将pageNum，pageSize绑定到当前线程对象上
        PageHelper.startPage(pageNum,pageSize);
        RouteExample routeExample = new RouteExample();
        RouteExample.Criteria criteria = routeExample.createCriteria();
        //多条件查询
        //拼接分类信息
        if (!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())){
            criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
        }
        //拼接路线信息
        if (!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            criteria.andRouteNameLike("%" + routeVo.getRouteName());
        }
        //是否主体之旅
        if (!EmptyUtil.isNullOrEmpty(routeVo.getIsThemeTour())){
            criteria.andIsThemeTourEqualTo(routeVo.getIsThemeTour());
        }
        //拼接最低价格
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())){
            criteria.andPriceGreaterThan(routeVo.getMinPrice());
        }
        //拼接最高价格
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())){
            criteria.andPriceLessThan(routeVo.getMaxPrice());
        }
        //注意这里的字段为数据库字段：类似example.setOrderByClause("index ASC,created_time ASC");
//        if (!EmptyUtil.isNullOrEmpty(routeVo.getOrderBy())){
//            example.setOrderByClause(routeVo.getOrderBy());
//        }

        List<Route> routes = routeMapper.selectByExample(routeExample);
        PageInfo<Route> pageInfo = new PageInfo<>(routes);
        PageInfo<RouteVo> pageInfoVo = new PageInfo<>();
        //外部对象拷贝
        BeanConv.toBean(pageInfo,pageInfoVo);
        //结果集转换
        List<RouteVo> routeVoList = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        pageInfoVo.setList(routeVoList);
        //处理图片
        for (RouteVo routeVoHandler : routeVoList) {
            AffixVo affixVo = AffixVo.builder().businessId(routeVoHandler.getId()).build();
            routeVoHandler.setAffixVoList(affixService.findAffixByBusinessId(affixVo));
        }
        return pageInfoVo;
        */
    }
}
